﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.Streaming;
using NPOI.XSSF.UserModel;
using Org.BouncyCastle.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 紧缩场反射面装调测试系统.ViewModels;
using UserControl = System.Windows.Controls.UserControl;

namespace 紧缩场反射面装调测试系统.Views
{
    /// <summary>
    /// DataAnalysis.xaml 的交互逻辑
    /// </summary>
    public partial class ResultAnalysis : UserControl
    {
        public static ResultAnalysisViewModel ViewModel { get; set; } = new ResultAnalysisViewModel();
        public ResultAnalysis()
        {
            InitializeComponent();
            this.DataContext = ViewModel;
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }



        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
        
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //double F = double.Parse(ViewModel.SelectFrequency);
            //string F_str = "";
            //if (F / 1000000000 >= 1)
            //{
            //    //GHz
            //    F_str = (F / 1000000000).ToString() + "GHz";
            //}
            //else if (F / 1000000 >= 1)
            //{
            //    //MHz
            //    F_str = (F / 1000000).ToString() + "MHz";
            //}
            //else if(F / 1000 >= 1)
            //{
            //    //KHz
            //    F_str = (F / 1000).ToString() + "KHz";
            //}
            //else
            //{
            //    F_str = F.ToString() + "Hz";
            //}

            //string lsh = "";
            //int ls = 1;
            //string FileFullName = SystemManage.SystemConfig.DataDirectory + "\\" + ViewModel.EXName + "_频点" + F_str + "分析结果" + lsh + ".xlsx";
            //while (true)
            //{
            //    if(System.IO.File.Exists(FileFullName))
            //    {
            //        lsh = "_" + ls.ToString();
            //        ls++;
            //        FileFullName = SystemManage.SystemConfig.DataDirectory + "\\" + ViewModel.EXName + "_频点" + F_str + "分析结果" + lsh + ".xlsx";
            //    }
            //    else
            //    {
            //        break;
            //    }
            //}


            //XSSFWorkbook WB = new XSSFWorkbook();
            //var style = WB.CreateCellStyle();
            //style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; // 设置水平居中
            //style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; // 设置垂直居中

            //ISheet Sheet = WB.CreateSheet("反射面数据分析结果");
            //bool IsTitle = true;

            ////导出数据
            //if(ViewModel.ResultE==null || ViewModel.ResultP==null)
            //{
            //    throw new Exception("没有数据可以导出!");
            //}



            //for(int i=0;i<ViewModel.ResultE.X.Length+1;i++)
            //{
            //    IRow Row = Sheet.CreateRow(i);
            //    if(IsTitle)
            //    {
            //        ICell Cell = Row.CreateCell(0);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue("位置");

            //        Cell = Row.CreateCell(1);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue("幅度");

            //        Cell = Row.CreateCell(2);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue("幅度拟合曲线");

            //        Cell = Row.CreateCell(3);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue("幅度纹波");

            //        Cell = Row.CreateCell(4);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue("相位");

            //        Cell = Row.CreateCell(5);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue("相位拟合曲线");

            //        Cell = Row.CreateCell(6);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue("相位纹波");
            //        IsTitle = false;
            //    }
            //    else
            //    {
            //        ICell Cell = Row.CreateCell(0);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue(ViewModel.ResultE.X[i-1]);

            //        Cell = Row.CreateCell(1);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue(ViewModel.ResultE.Y[i - 1]);

            //        Cell = Row.CreateCell(2);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue(ViewModel.ResultE.Y_Fit[i - 1]);

            //        Cell = Row.CreateCell(3);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue(ViewModel.ResultE.Y[i - 1]- ViewModel.ResultE.Y_Fit[i - 1]);

            //        Cell = Row.CreateCell(4);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue(ViewModel.ResultP.Y[i - 1]);

            //        Cell = Row.CreateCell(5);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue(ViewModel.ResultP.Y_Fit[i - 1]);

            //        Cell = Row.CreateCell(6);
            //        Cell.CellStyle = style;
            //        Cell.SetCellType(CellType.String);
            //        Cell.SetCellValue(ViewModel.ResultP.Y[i - 1] - ViewModel.ResultP.Y_Fit[i - 1]);
            //    }
                
       
            //}

            ////设置列宽
            //int ColumnWidth = 15; // 设置单元格宽度为 20 个字符
            //Sheet.SetColumnWidth(1, ColumnWidth * 256); // 设置第一列的宽度
            //Sheet.SetColumnWidth(2, ColumnWidth * 256); // 设置第一列的宽度
            //Sheet.SetColumnWidth(3, ColumnWidth * 256); // 设置第一列的宽度
            //Sheet.SetColumnWidth(4, ColumnWidth * 256); // 设置第一列的宽度
            //Sheet.SetColumnWidth(5, ColumnWidth * 256); // 设置第一列的宽度
            //Sheet.SetColumnWidth(6, ColumnWidth * 256); // 设置第一列的宽度

            ////插入截图
            //for (int i = 0; i < 4; i++)
            //{
            //    // 获取要截图的 UIElement
            //    UIElement element = (UIElement)FindName($"jt{i+1}");

            //    // 创建一个 RenderTargetBitmap，指定要截图的区域和分辨率
            //    Rect rect = new Rect(element.RenderSize);
            //    RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Width, (int)rect.Height, 96, 96, PixelFormats.Default);

            //    // 使用 RenderTargetBitmap 渲染要截图的区域
            //    DrawingVisual drawingVisual = new DrawingVisual();
            //    using (DrawingContext drawingContext = drawingVisual.RenderOpen())
            //    {
            //        VisualBrush visualBrush = new VisualBrush(element);
            //        drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(), rect.Size));
            //    }
            //    rtb.Render(drawingVisual);

            //    // 创建一个 BitmapEncoder，用于保存截图为图像文件
            //    BitmapEncoder encoder = new PngBitmapEncoder();
            //    encoder.Frames.Add(BitmapFrame.Create(rtb));

            //    MemoryStream stream = new MemoryStream();
            //    encoder.Save(stream);

            //    // 将图像数据添加到 Excel 文档中
            //    int offset = 0;
            //    if(i%2==1)
            //    {
            //        offset = 1;
            //    }

            //    int pictureIndex = WB.AddPicture(stream.ToArray(), XSSFWorkbook.PICTURE_TYPE_PNG);

            //    // 创建绘图对象
            //    XSSFDrawing drawing = (XSSFDrawing)Sheet.CreateDrawingPatriarch();

            //    // 创建锚点
            //    XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 8+offset, 0+20*i+i, 18-offset, 20+20*i+i);

            //    // 创建图片对象
            //    XSSFPicture picture = (XSSFPicture)drawing.CreatePicture(anchor, pictureIndex);

            //    // 设置图片大小
            //    //picture.Resize();

            //    // 保存截图为图像文件
            //    //using (var fileStream = new System.IO.FileStream($"C:\\Users\\Shipengfei\\Desktop\\screenshot{i}.png", System.IO.FileMode.Create))
            //    //{
            //    //    encoder.Save(fileStream);
            //    //}
            //}

            ////保存文件
            //Stream Stream = new FileStream(FileFullName, FileMode.Create, FileAccess.ReadWrite);
            //WB.Write(Stream);
            //Stream.Close();
            //HandyControl.Controls.Growl.Success($"{FileFullName}已生成!");
        }
    }
}
